//
//  ZChealthyModelContent.m
//
//  Created by   on 16/1/18
//  Copyright (c) 2016 __MyCompanyName__. All rights reserved.
//

#import "ZChealthyModelContent.h"


NSString *const kZChealthyModelContentAction = @"action";
NSString *const kZChealthyModelContentActionName = @"action_name";
NSString *const kZChealthyModelContentCtime = @"ctime";
NSString *const kZChealthyModelContentId = @"id";
NSString *const kZChealthyModelContentMid = @"mid";
NSString *const kZChealthyModelContentSid = @"sid";
NSString *const kZChealthyModelContentBeans = @"beans";


@interface ZChealthyModelContent ()

- (id)objectOrNilForKey:(id)aKey fromDictionary:(NSDictionary *)dict;

@end

@implementation ZChealthyModelContent

@synthesize action = _action;
@synthesize actionName = _actionName;
@synthesize ctime = _ctime;
@synthesize contentIdentifier = _contentIdentifier;
@synthesize mid = _mid;
@synthesize sid = _sid;
@synthesize beans = _beans;


+ (instancetype)modelObjectWithDictionary:(NSDictionary *)dict
{
    return [[self alloc] initWithDictionary:dict];
}

- (instancetype)initWithDictionary:(NSDictionary *)dict
{
    self = [super init];
    
    // This check serves to make sure that a non-NSDictionary object
    // passed into the model class doesn't break the parsing.
    if(self && [dict isKindOfClass:[NSDictionary class]]) {
            self.action = [self objectOrNilForKey:kZChealthyModelContentAction fromDictionary:dict];
            self.actionName = [self objectOrNilForKey:kZChealthyModelContentActionName fromDictionary:dict];
            self.ctime = [self objectOrNilForKey:kZChealthyModelContentCtime fromDictionary:dict];
            self.contentIdentifier = [self objectOrNilForKey:kZChealthyModelContentId fromDictionary:dict];
            self.mid = [self objectOrNilForKey:kZChealthyModelContentMid fromDictionary:dict];
            self.sid = [self objectOrNilForKey:kZChealthyModelContentSid fromDictionary:dict];
            self.beans = [self objectOrNilForKey:kZChealthyModelContentBeans fromDictionary:dict];

    }
    
    return self;
    
}

- (NSDictionary *)dictionaryRepresentation
{
    NSMutableDictionary *mutableDict = [NSMutableDictionary dictionary];
    [mutableDict setValue:self.action forKey:kZChealthyModelContentAction];
    [mutableDict setValue:self.actionName forKey:kZChealthyModelContentActionName];
    [mutableDict setValue:self.ctime forKey:kZChealthyModelContentCtime];
    [mutableDict setValue:self.contentIdentifier forKey:kZChealthyModelContentId];
    [mutableDict setValue:self.mid forKey:kZChealthyModelContentMid];
    [mutableDict setValue:self.sid forKey:kZChealthyModelContentSid];
    [mutableDict setValue:self.beans forKey:kZChealthyModelContentBeans];

    return [NSDictionary dictionaryWithDictionary:mutableDict];
}

- (NSString *)description 
{
    return [NSString stringWithFormat:@"%@", [self dictionaryRepresentation]];
}

#pragma mark - Helper Method
- (id)objectOrNilForKey:(id)aKey fromDictionary:(NSDictionary *)dict
{
    id object = [dict objectForKey:aKey];
    return [object isEqual:[NSNull null]] ? nil : object;
}


#pragma mark - NSCoding Methods

- (id)initWithCoder:(NSCoder *)aDecoder
{
    self = [super init];

    self.action = [aDecoder decodeObjectForKey:kZChealthyModelContentAction];
    self.actionName = [aDecoder decodeObjectForKey:kZChealthyModelContentActionName];
    self.ctime = [aDecoder decodeObjectForKey:kZChealthyModelContentCtime];
    self.contentIdentifier = [aDecoder decodeObjectForKey:kZChealthyModelContentId];
    self.mid = [aDecoder decodeObjectForKey:kZChealthyModelContentMid];
    self.sid = [aDecoder decodeObjectForKey:kZChealthyModelContentSid];
    self.beans = [aDecoder decodeObjectForKey:kZChealthyModelContentBeans];
    return self;
}

- (void)encodeWithCoder:(NSCoder *)aCoder
{

    [aCoder encodeObject:_action forKey:kZChealthyModelContentAction];
    [aCoder encodeObject:_actionName forKey:kZChealthyModelContentActionName];
    [aCoder encodeObject:_ctime forKey:kZChealthyModelContentCtime];
    [aCoder encodeObject:_contentIdentifier forKey:kZChealthyModelContentId];
    [aCoder encodeObject:_mid forKey:kZChealthyModelContentMid];
    [aCoder encodeObject:_sid forKey:kZChealthyModelContentSid];
    [aCoder encodeObject:_beans forKey:kZChealthyModelContentBeans];
}

- (id)copyWithZone:(NSZone *)zone
{
    ZChealthyModelContent *copy = [[ZChealthyModelContent alloc] init];
    
    if (copy) {

        copy.action = [self.action copyWithZone:zone];
        copy.actionName = [self.actionName copyWithZone:zone];
        copy.ctime = [self.ctime copyWithZone:zone];
        copy.contentIdentifier = [self.contentIdentifier copyWithZone:zone];
        copy.mid = [self.mid copyWithZone:zone];
        copy.sid = [self.sid copyWithZone:zone];
        copy.beans = [self.beans copyWithZone:zone];
    }
    
    return copy;
}


@end
